<script setup>

import {ref, onMounted, watch, computed} from 'vue';
import axios from "axios";
import { message } from 'ant-design-vue';
import moment from "moment"

// 查询
const pageSize = ref(10);
const pageNo = ref(1)
const items = ref([]);
const loading = ref(true);
const confirmLoading = ref(false);
const totalItems = ref(0);
const pagination = computed(() => ({
    total: totalItems.value,
    current: pageNo.value,
    pageSize: pageSize.value
})) 
const queryParams = ref({
    startTime: "",
    endTime: "",
    orderNumber: ""
});
const columns = ref([
{
        title: "返点清单编码",
        dataIndex: "orderNumber",
        key: 'orderNumber'
    },
    {
        title: "返点清单名称",
        dataIndex: "orderName",
        key: 'orderName'
    },
    {
        title: "上传时间",
        dataIndex: "createTime",
        key: 'createTime'
    },
    {
        title: "修改记录",
        dataIndex: "updateTime",
        key: 'updateTime'
    },
    {
        title: "含税金额(元)",
        dataIndex: "taxTotalAmount",
        key: 'taxTotalAmount',
        align: "center"
    },
    {
        title: "返点金额(元)",
        dataIndex: "pointAmount",
        key: 'pointAmount',
        align: "center"
    },
    {
        title: "实返总额(元)",
        dataIndex: "trueAmount",
        key: 'trueAmount',
        align: "center"
    },
    {
        title: "未返总额(元)",
        dataIndex: "unAmount",
        key: 'unAmount',
        align: "center"
    },
    {
        title: "操作人员",
        dataIndex: "username",
        key: 'username',
        align: "center"
    },
    {
        title: "操作",
        dataIndex: "action",
        width: '140px'
    }
])
const columnsAdd = ref([
    {
        title: "商品编码",
        dataIndex: "goodsNumber",
        key: 'goodsNumber',
        fixed: "left"
    },
    {
        title: "商品名",
        dataIndex: "goodsName",
        key: 'goodsName',
        fixed: "left"
    },
    {
        title: "通用名称",
        dataIndex: "commonName",
        key: 'commonName'
    },
    {
        title: "供应商名称",
        dataIndex: "supplierName",
        key: 'supplierName'
    },
    {
        title: "返点率(%)",
        dataIndex: "pointRate",
        key: 'pointRate',
        align: "center"
    },
    {
        title: "税率(%)",
        dataIndex: "taxRate",
        key: 'taxRate',
        align: "center"
    },
    {
        title: "税额(元)",
        dataIndex: "taxAmount",
        key: 'taxAmount',
        align: "center"
    },
    {
        title: "含税均价(元)",
        dataIndex: "taxAveragePrice",
        key: 'taxAveragePrice',
        align: "center"
    },
    {
        title: "含税总额(元)",
        dataIndex: "taxTotalAmount",
        key: 'taxTotalAmount',
        align: "center"
    },
    {
        title: "无税均价(元)",
        dataIndex: "noTaxAveragePrice",
        key: 'noTaxAveragePrice',
        align: "center"
    },
    {
        title: "无税总额(元)",
        dataIndex: "noTaxTotalAmount",
        key: 'noTaxTotalAmount',
        align: "center"
    },
    {
        title: "零售价(元)",
        dataIndex: "retailPrice",
        key: 'retailPrice',
        align: "center"
    },
    {
        title: "购进数量",
        dataIndex: "num",
        key: 'num',
        align: "center"
    },
    {
        title: "结算金额(元)",
        dataIndex: "payAmount",
        key: 'payAmount',
        align: "center"
    },
    {
        title: "实返金额(元)",
        dataIndex: "trueAmount",
        key: 'trueAmount',
        align: "center"
    },
    {
        title: "未返金额(元)",
        dataIndex: "unAmount",
        key: 'unAmount',
        align: "center"
    },
    {
        title: "操作人员",
        dataIndex: "username",
        key: 'username',
        align: "center"
    },
    {
        title: "返点金额(元)",
        dataIndex: "pointAmount",
        key: 'pointAmount',
        align: "center",
        fixed: 'right'
    },
    {
        title: "登记金额(元)",
        dataIndex: "registerAmount",
        key: 'registerAmount',
        align: "center",
        fixed: 'right'
    },
])
const open = ref(false);
const title = ref("");
const formData = ref({});

function loadItems({page, itemsPerPage}) {
    loading.value = true;
    pageNo.value = page
    pageSize.value = itemsPerPage
    axios.post('/MedicineOrderRegister/getMedicineOrderRegisterList', {
        page: page,
        pageSize: itemsPerPage,
        orderNumber: queryParams.value.orderNumber,
        startTime: queryParams.value.startTime ? moment(queryParams.value.startTime.$d).format("YYYY-MM-DD HH:mm:ss") : '',
        endTime: queryParams.value.endTime ? moment(queryParams.value.endTime.$d).format("YYYY-MM-DD HH:mm:ss"): ""
    }).then(({data}) => {
        items.value = data.data.records;
        totalItems.value = data.data.total;
        loading.value = false;
    })
}

onMounted(() => {
    loadItems({page: 1, itemsPerPage: pageSize.value});
});

const dialogEnum = {
    update: "编辑",
    create: "新增"
}

const recordList = ref([])
function openData(data) {
    open.value = true
    title.value = data.orderName
    let obj = {
        orderNumber: data.orderNumber
    }
    axios.post("/MedicineOrderRegister/getMedicineOrderRegisterInfo", obj).then(res => {
        recordList.value = res.data.data
    })
}

function submit() {
    let obj = {
        medicineOrderRegisterList: recordList.value
    }
    obj.medicineOrderRegisterList.forEach(record => {
        record.noTaxTotalAmount = (record.noTaxAveragePrice * record.num).toFixed(2)
        record.trueAmount = (record.trueAmount + Number(record.registerAmount)).toFixed(2)
        record.unAmount = (record.unAmount - Number(record.registerAmount)).toFixed(2)
        record.taxTotalAmount = (record.taxAveragePrice * record.num).toFixed(2)
        record.taxAmount = (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num).toFixed(2)
        record.pointAmount = (record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2)
        record.payAmount = (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2)
    })
    axios.post('/MedicineOrderRegister/registerMedicineOrder', obj).then(res => {
        if(res.data.code == 0) {
            message.success('登记成功！')
            loading.value = false
            open.value = false
        } else {
            message.error(res.data.message)
            loading.value = false
        }
    }, err => {
        loading.value = false
        message.error(err.data.message)
    })
}

function handleTabChange(pag){
    loadItems({page: pag.current, itemsPerPage: pag.pageSize})
}

const totalNum = computed(() => {
    const num = recordList.value.reduce((sum, record) => sum + (record.noTaxAveragePrice * record.num * (record.pointRate/100)), 0)
    return num.toFixed(2);
})

const totalNumRegister = computed(() => {
    const num = recordList.value.reduce((sum, record) => sum + Number(record.registerAmount), 0)
    return num.toFixed(2);
})
</script>

<template>
    <a-spin size="large" :spinning="loading">
        <a-card title="按清单管理">
            <a-row style="margin-bottom: 25px;">
                <a-form
                layout="inline"
                :model="queryParams"
                :colon="false">
                    <a-form-item label="返点清单编码">
                        <a-input v-model:value="queryParams.orderNumber" placeholder="返点清单编码"  allow-clear @keyup.enter="loadItems({page: 1, itemsPerPage: pageSize})"></a-input>
                    </a-form-item>
                    <a-form-item label="开始时间">
                        <a-date-picker show-time v-model:value="queryParams.startTime" placeholder="开始时间"></a-date-picker>
                    </a-form-item>
                    <a-form-item label="结束时间">
                        <a-date-picker show-time v-model:value="queryParams.endTime" placeholder="结束时间"></a-date-picker>
                    </a-form-item>
                    <a-from-item>
                        <a-button @click="loadItems({page: 1, itemsPerPage: pageSize})">查询</a-button>
                    </a-from-item>
                </a-form>
            </a-row>
            <a-table
            :dataSource="items"
            :columns="columns"
            :pagination="pagination"
            @change="handleTabChange">
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex == 'pointRate'">
                        <span>{{ (record.pointRate).toFixed() }}%</span>
                    </template>
                    <template v-if="column.dataIndex == 'taxRate'">
                        <span>{{ (record.taxRate).toFixed() }}%</span>
                    </template>
                    <tempalte v-if="column.dataIndex == 'status'">
                        <span v-if="record.status">使用中</span>
                        <span v-if="!record.status">未使用</span>
                    </tempalte>
                    <tempalte v-if="column.dataIndex == 'action'">
                        <a @click="openData(record)">查看详情</a>
                    </tempalte>
                </template>
            </a-table>            
        </a-card>

        <a-modal
        v-model:open="open"
        :title="title"
        cancelText="关闭"
        okText="确定"
        width="76%"
        zIndex=5000
        :confirmLoading="confirmLoading"
        @ok="submit">
            <a-table
            :dataSource="recordList"
            :columns="columnsAdd"
            :scroll="{ x: 2000 }"
            :pagination="false">
                <template #bodyCell="{column, record, index}">
                    <template v-if="column.dataIndex == 'pointRate'">
                        <a-input v-model:value="record.pointRate"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'taxRate'">
                        <a-input v-model:value="record.taxRate"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'taxAveragePrice'">
                        <a-input v-model:value="record.taxAveragePrice"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'noTaxAveragePrice'">
                        <a-input v-model:value="record.noTaxAveragePrice"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'retailPrice'">
                        <a-input v-model:value="record.retailPrice"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'num'">
                        <a-input v-model:value="record.num"></a-input>
                    </template>
                    <template v-if="column.dataIndex == 'noTaxTotalAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'trueAmount'">
                        <span>{{ (record.trueAmount + Number(record.registerAmount)).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'unAmount'">
                        <span>{{ (record.unAmount - Number(record.registerAmount)).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'taxTotalAmount'">
                        <!-- <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100))).toFixed(2) }}</span> -->
                        <span>{{ (record.taxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'taxAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'pointAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'payAmount'">
                        <span>{{ (record.noTaxAveragePrice * record.num * (1 + (record.taxRate/100)) - record.noTaxAveragePrice * record.num * (record.pointRate/100)).toFixed(2) }}</span>
                    </template>
                    <template v-if="column.dataIndex == 'registerAmount'">
                        <a-input v-model:value="record.registerAmount"></a-input>
                    </template>
                </template>
                <template #summary>
                    <a-table-summary-row>
                        <a-table-summary-cell :index="0" :col-span="2">
                            <div style="font-weight: 700;">合计</div>
                        </a-table-summary-cell>
                        <a-table-summary-cell :index="2" :col-span="15"></a-table-summary-cell>
                        <a-table-summary-cell :index="17">
                            <div style="text-align: center;font-weight: 700;">返点金额：{{ totalNum }}</div>
                        </a-table-summary-cell>
                        <a-table-summary-cell :index="18">
                            <div style="text-align: center;font-weight: 700;">登记金额：{{ totalNumRegister }}</div>
                        </a-table-summary-cell>
                    </a-table-summary-row>
                </template>
            </a-table>
        </a-modal>
    </a-spin>
</template>

<style scoped lang="less">

</style>
